<?php

class BaseZF_Framework_Log_Writer_Firebug extends Zend_Log_Writer_Abstract
{
    private $_events;

    /**
     * Class Constructor
     */
    public function __construct()
    {
    }

    /**
     * Write a message to the log.
     *
     * @param  array  $event  event data
     * @return void
     */
    protected function _write($event)
    {
        $this->_events[] = $event;
    }

    /**
     *
     */
    protected function _render()
    {
        $buffer_groups = array(
            BaseZF_Framework_Log::DEBUG_GLOBAL => array(),
        );

        foreach ($this->_events as $key => $event) {

            $group_id = $event['priority'];
            $message =  str_replace("\n", '\n', $event['message']);

            switch ($group_id) {
                case Zend_Log::EMERG:
                case Zend_Log::ALERT:
                case Zend_Log::CRIT:
                case Zend_Log::ERR:
                    $buffer_groups[BaseZF_Framework_Log::DEBUG_GLOBAL][] = 'console.error("' . $message . '");';
                    break;

                case Zend_Log::WARN:
                    $buffer_groups[BaseZF_Framework_Log::DEBUG_GLOBAL][] = 'console.warn("' . $message . '");';
                    break;

                case Zend_Log::NOTICE:
                case Zend_Log::INFO:
                case Zend_Log::DEBUG:
                    $buffer_groups[BaseZF_Framework_Log::DEBUG_GLOBAL][] = 'console.info("' . $message . '");';
                    break;

                case BaseZF_Framework_Log::DEBUG_PHP:
                    // @todo func
                case BaseZF_Framework_Log::DEBUG_PDOSTMT:
                case BaseZF_Framework_Log::DEBUG_SESSION:
                case BaseZF_Framework_Log::DEBUG_APCCACHE: //@todo
                case BaseZF_Framework_Log::DEBUG_MEMCACHE: //@todo

                    // add array group on $buffer_groups is not exist
                    if (!isset($buffer_groups[$group_id])) {
                        $buffer_groups[$group_id] = array();
                    }

                    $buffer_groups[$group_id][] = 'console.info("' . $message . '");';
                    break;

                default:
            }
        }

        $output = array();

        // open script tag
        $output[] = '';
        $output[] = '<script>';

        // render standart buffer
        foreach ($buffer_groups as $group_id => $log_messages) {

            $group_name = BaseZF_Framework_Log::getlogGroupNames($group_id);

            // start group
            $output[] = 'console.group("' . $group_name . '")';
            $output[] = implode("\n", $log_messages);
            $output[] = 'console.groupEnd();';
        }

        // close script tag
        $output[] = '</script>';

        // render
        return implode("\n", $output);
    }

    /**
     * Display on standart output
     */
    public function __destruct() {

        $view = BaseZF::registry('view');

        if (!$view->isAjax()) {
            echo $this->_render();
        }
    }
}
